package com.csmht.tododemo.controller.user;

import com.csmht.tododemo.annotation.RoleIntercept;
import com.csmht.tododemo.mapper.ReportMapper;
import com.csmht.tododemo.pojo.dto.CommentDTO;
import com.csmht.tododemo.pojo.dto.CommentPageDTO;
import com.csmht.tododemo.pojo.entity.Report;
import com.csmht.tododemo.pojo.enumeration.RoleEnum;
import com.csmht.tododemo.pojo.vo.CommentVO;
import com.csmht.tododemo.result.PageResult;
import com.csmht.tododemo.result.Result;
import com.csmht.tododemo.service.CommentService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.csmht.tododemo.constant.MessageConstant.DATA_ERR;

@RestController
@RequestMapping("/user/comment")
@Slf4j
@RoleIntercept(roles = {RoleEnum.ADMIN, RoleEnum.USER})
public class CommentController {

    @Resource
    private CommentService  commentService;

    @PutMapping
    public Result<CommentVO> add(@RequestBody CommentDTO commentDTO) {

        if(     (commentDTO.getCommentMainId() == null && commentDTO.getCommentId() != null)||
                (commentDTO.getCommentMainId() != null && commentDTO.getCommentId() == null)){
            return Result.error(DATA_ERR);
        }

        CommentVO commentVO = commentService.add(commentDTO);
        return Result.success(commentVO);
    }

    @GetMapping
    public PageResult<CommentVO> mainList(CommentPageDTO commentPageDTO) {
        List<CommentVO> ans = commentService.mainList(commentPageDTO);
        return PageResult.success(ans);
    }


    @GetMapping("/{id}")
    private PageResult<CommentVO> List(@PathVariable Long id,CommentPageDTO commentPageDTO) {
        List<CommentVO> ans = commentService.List(id,commentPageDTO);
        return PageResult.success(ans);
    }
}
